package com.beiyou.service.impl;

import com.beiyou.controller.UserActivityServlet;
import com.beiyou.mapper.UserMapper;
import com.beiyou.model.Activity;
import com.beiyou.model.User;
import com.beiyou.service.Uservice;
import com.beiyou.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * 用户管理的代理类（实现类）
 */
public class UserServiceImpl implements Uservice {

    @Override
    public List<User> getAllUser() {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getAllUser();
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return userList;
    }

    @Override
    public int addUser(User user) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int result = userMapper.addUser(user);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }

    @Override
    public int delUser(int id, String Username) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int result = userMapper.delUser(id,Username);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }

    @Override
    public int updateUser(Map<String, Object> userMap) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int result = userMapper.updateUser(userMap);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return result;
    }

    /**
     * 模糊查询
     */
    @Override
    public List<User> getUserByCon(User user) {
        // 获取mybatis数据库会话对象
        SqlSession sqlSession = MybatisUtil.openSession();
        // 开启事务
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserByCon(user);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
        return userList;
    }
}
